package com.netease.nim.demo.session.viewholder;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.netease.nim.demo.common.ui.imageview.MsgThumbImageView;
import com.netease.nim.demo.common.util.media.ImageUtil;
import com.netease.nim.demo.util.AttachmentStore;
import com.netease.nimlib.sdk.msg.attachment.VideoAttachment;
import com.netease.nimlib.sdk.msg.constant.AttachStatusEnum;
import com.netease.nimlib.sdk.msg.model.IMMessage;
import com.shwread.android.qysw10000204.R;

public class MsgViewHolderVideoLeft extends MsgViewHolder {

	public MsgThumbImageView thumbnailImageView;

	protected ImageView maskImageView;

	public ProgressBar progress;

	protected int getResId() {
		return R.layout.video_message_view_left_item;
	}

	@Override
	protected int getViewHolderDirection() {
		return VIEW_HOLDER_LEFT;
	}

	public void inflate() {
		super.inflate();

		progress = (ProgressBar) view.findViewById(R.id.sns_audio_download_progressBar);
		thumbnailImageView = (MsgThumbImageView) view.findViewById(R.id.imageViewThumbnail);
		longClickView = thumbnailImageView;
		maskImageView = (ImageView) view.findViewById(R.id.imageViewMask);
	}

	public void refresh(Object item) {
		super.refresh(item);
		setLayoutParams(getImageDefWidth(), getImageDefHeight(), maskImageView, thumbnailImageView);

		final IMMessage message = messageItem.getMessage();
		VideoAttachment msgAttachment = (VideoAttachment) messageItem.getMessage().getAttachment();

		String thumbPath = msgAttachment.getThumbPath();
		String videoPath = msgAttachment.getPath();

		if (!TextUtils.isEmpty(thumbPath)) {
			loadThumbnailImage(thumbPath);
			alertButton.setVisibility(View.GONE);
			progress.setVisibility(View.GONE);
		} else {
			AttachStatusEnum status = messageItem.getMessage().getAttachStatus();
			switch (status) {
			case def:
			case transferred:
				thumbnailImageView.loadAsResource(R.drawable.default_img, 30, 20, R.drawable.message_left_white_bg,
						R.drawable.message_left_white_cover_bg);
				alertButton.setVisibility(View.GONE);
				progress.setVisibility(View.GONE);
				redownload();
				break;
			case transferring:
				thumbnailImageView.loadAsResource(R.drawable.default_img, 30, 20, R.drawable.message_left_white_bg,
						R.drawable.message_left_white_cover_bg);
				alertButton.setVisibility(View.GONE);
				progress.setVisibility(View.VISIBLE);
				break;
			case fail:
				thumbnailImageView.loadAsResource(R.drawable.default_img_failed, 30, 20, R.drawable.message_left_white_bg,
						R.drawable.message_left_white_cover_bg);
				alertButton.setVisibility(View.VISIBLE);
				progress.setVisibility(View.GONE);
				break;
			}
		}

		thumbnailImageView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
                //TODO fw WatchVideoActivity
				//WatchVideoActivity.start(context, message);
			}
		});
	}

	private void loadThumbnailImage(String thumbPath) {
		if (!AttachmentStore.isFileExist(thumbPath)) {
			setLayoutParams(getImageDefWidth(), getImageDefHeight(), thumbnailImageView, maskImageView);
		} else {
			int[] bound = ImageUtil.getBoundWithLength(getImageMaxEdge(), thumbPath, true);
			int width = bound[0];
			int height = bound[1];

			setLayoutParams(width, height, maskImageView, thumbnailImageView);
			thumbnailImageView.setImageBitmap(thumbnailImageView.loadAsPath(thumbPath, width, height,
					R.drawable.message_left_white_bg,
					R.drawable.message_left_white_cover_bg));
		}
	}
}
